package com.hisan.waimaidao.carvas;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;


public class MainActivity extends AppCompatActivity {
    private ProgressDialog progressDialog;
    private ProgressDialog.Builder builder;
    private int progress =0;
    private int mTime =1000;
    private Timer mTimer;
    private TimerTask mTimerTask;
    private boolean isonchlick=true;



    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    if (progress<=100){
                        progress++;
                        if (progressDialog!=null){
                            builder.getProgress().start(progress);
                        }
                    }else {
                        handler.removeMessages(0);
                    }
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        builder = new ProgressDialog.Builder(this);
        progressDialog = builder.create();
        final Button button= (Button) findViewById(R.id.onstart);
        Button button1= (Button) findViewById(R.id.onstop);
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                Message msg= handler.obtainMessage();
                msg.what = 0;
                handler.sendMessage(msg);
            }
        };
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isonchlick){
                    button.setText("暂停");
                    isonchlick=false;
                }else {
                    button.setText("开始");
                    isonchlick=true;
                }

                if(mTimerTask !=null){
                    mTimerTask.cancel();
                }
                mTimer.purge();
                if (!isonchlick){
                    progressDialog.show();
                    mTimerTask = new TimerTask() {
                        @Override
                        public void run() {
                            Message msg= handler.obtainMessage();
                            msg.what = 0;
                            handler.sendMessage(msg);
                        }
                    };
                    mTimer.schedule(mTimerTask,mTime,mTime);
                }
            }
        });

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mTimerTask !=null){
                    mTimerTask.cancel();
                }
                mTimer.purge();
                progress=0;
                mTimerTask = new TimerTask() {
                    @Override
                    public void run() {
                        Message msg= handler.obtainMessage();
                        msg.what = 0;
                        handler.sendMessage(msg);
                    }
                };
                mTimer.schedule(mTimerTask,mTime,mTime);
                progressDialog.show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}
